package com.cloudeasy.area.util;

import com.cloudeasy.api.vo.area.AreaVO;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.area.po.Area;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 区域工具类
 */
public class AreaUtil {
    /**
     * 转换VO对象
     *
     * @param area {@link Area}
     * @return {@link AreaVO}
     */
    public static AreaVO convert(Area area) {
        AreaVO areaVO = new AreaVO();
        BeanUtils.copyProperties(area, areaVO);
        return areaVO;
    }

    /**
     * 转换VO对象
     *
     * @param areas {@link List<Area>}
     * @return {@link List<AreaVO>}
     */
    public static List<AreaVO> convert(List<Area> areas) {
        return areas.stream().map(AreaUtil::convert).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param page {@link Page<Area>}
     * @return {@link PageResult<AreaVO>}
     */
    public static PageResult<AreaVO> convert(Page<Area> page) {
        PageResult<AreaVO> areaVOPageResult = new PageResult<>();
        areaVOPageResult.setTotal(page.getTotalElements())
                .setPages((long) page.getTotalPages())
                .setRecords(convert(page.getContent()));
        return areaVOPageResult;
    }
}
